#include "UserInfoDialog.h"
#include "../../core/Impp.h"

namespace Impp
{

std::vector<UserInfoDialog*> UserInfoDialog::openedWindows;

UserInfoDialog::UserInfoDialog(wxWindow* parent, Buddy* buddy) :
		wxFrame(parent,wxID_ANY,wxT("User Info")),buddy(buddy)
{
	infos = new wxRichTextCtrl(this,wxID_ANY,wxEmptyString,wxDefaultPosition,wxDefaultSize);

	update(buddy);

	//register self;
	UserInfoDialog::openedWindows.push_back(this);

	SetSize(600,400);
	Layout();
	CenterOnScreen();
	Show();

}

UserInfoDialog::~UserInfoDialog()
{

	//deregister self
	std::vector<UserInfoDialog*>::iterator dIter(UserInfoDialog::openedWindows.begin());
	for (; dIter != UserInfoDialog::openedWindows.end(); ++dIter) {
	      if(*dIter==this){
	    	  UserInfoDialog::openedWindows.erase(dIter);
	    	  break;
	      }
	}
}

void UserInfoDialog::updateOpenWindows(Buddy* buddy){
	std::vector<UserInfoDialog*>::iterator dIter(UserInfoDialog::openedWindows.begin());
	for (; dIter != UserInfoDialog::openedWindows.end(); ++dIter) {
	      (*dIter)->update(buddy);
	}

}

void UserInfoDialog::update(Buddy* buddy){
	if(this->buddy !=buddy){
		return;
	}else{

		infos->SetValue(wxEmptyString);

		//std::map<wxString, wxString> countryCodes;

		//countryCodes[wxT("0")] = wxT("blub");


		Category basic;
		basic.displayName = wxT("Basic User Info");
		basic.infos.push_back(Info(BUDDY_EXT_NICKNAME, wxT("Nickname: %s\n")));
		basic.infos.push_back(Info(BUDDY_EXT_FIRSTNAME, wxT("Name: %s\n")));
		basic.infos.push_back(Info(BUDDY_EXT_LASTNAME, wxT("Lastname: %s\n")));
		basic.infos.push_back(Info(BUDDY_EXT_GENDER, wxT("Gender: %s\n")));
		basic.infos.push_back(Info(BUDDY_EXT_BIRTH_DAY, wxT("Birthday: %s.")));
		basic.infos.push_back(Info(BUDDY_EXT_BIRTH_MONTH, wxT("%s.")));
		basic.infos.push_back(Info(BUDDY_EXT_BIRTH_YEAR, wxT("%s\n")));
		basic.infos.push_back(Info(BUDDY_EXT_AGE, wxT("Age: %s\n")));
		basic.infos.push_back(Info(BUDDY_EXT_EMAIL, wxT("EMail: %s\n")));
		basic.infos.push_back(Info(BUDDY_EXT_HOMEPAGE, wxT("Homepage: %s\n")));

		Category interets;
		interets.displayName = wxT("Interests");
		interets.infos.push_back(Info(BUDDY_EXT_INTEREST1_KEYWORD, wxT("Interest 1: %s\n")));
		interets.infos.push_back(Info(BUDDY_EXT_INTEREST2_KEYWORD, wxT("Interest 2: %s\n")));
		interets.infos.push_back(Info(BUDDY_EXT_INTEREST3_KEYWORD, wxT("Interest 3: %s\n")));
		interets.infos.push_back(Info(BUDDY_EXT_INTEREST4_KEYWORD, wxT("Interest 4: %s\n")));

		Category lng;
		lng.displayName = wxT("Speaking Languages");
		lng.infos.push_back(Info(BUDDY_EXT_SPEAKING_LANGUAGE1, wxT("Language 1: %s\n")));
		lng.infos.push_back(Info(BUDDY_EXT_SPEAKING_LANGUAGE2, wxT("Language 2: %s\n")));
		lng.infos.push_back(Info(BUDDY_EXT_SPEAKING_LANGUAGE3, wxT("Language 3: %s\n")));

		Category home;
		home.displayName = wxT("Home Address");
		home.infos.push_back(Info(BUDDY_EXT_HOME_ADDRESS, wxT("Street: %s\n")));
		home.infos.push_back(Info(BUDDY_EXT_HOME_ZIP, wxT("ZIP: %s\n")));
		home.infos.push_back(Info(BUDDY_EXT_HOME_CITY, wxT("City: %s\n")));
		home.infos.push_back(Info(BUDDY_EXT_HOME_STATE, wxT("State: %s\n")));
		home.infos.push_back(Info(BUDDY_EXT_HOME_COUNTRY_CODE, wxT("Country: %s\n")));
		home.infos.push_back(Info(BUDDY_EXT_HOME_PHONE, wxT("Phone: %s\n")));
		home.infos.push_back(Info(BUDDY_EXT_HOME_FAX, wxT("Fax: %s\n")));


		Category notes;
		interets.displayName = wxT("Notes");
		interets.infos.push_back(Info(BUDDY_EXT_NOTES, wxT("%s\n")));

		Category org;
		org.displayName = wxT("Original From Address");
		org.infos.push_back(Info(BUDDY_EXT_ORIGINAL_FROM_CITY, wxT("City: %s\n")));
		org.infos.push_back(Info(BUDDY_EXT_ORIGINAL_FROM_STATE, wxT("State: %s\n")));
		org.infos.push_back(Info(BUDDY_EXT_ORIGINAL_FROM_COUNTRY_CODE, wxT("Country: %s\n")));

		Category work;
		work.displayName = wxT("Working Address");
		work.infos.push_back(Info(BUDDY_EXT_WORK_ADDRESS, wxT("Street: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_ZIP, wxT("ZIP: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_CITY, wxT("City: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_STATE, wxT("State: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_POSITION, wxT("Position: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_DEPARTMENT, wxT("Department: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_COUNTRY_CODE, wxT("Country: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_PHONE, wxT("Phone: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_FAX, wxT("Fax: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_HOMEPAGE, wxT("Homepage: %s\n")));
		work.infos.push_back(Info(BUDDY_EXT_WORK_COMPANY, wxT("Company: %s\n")));

		writeIn(basic);
		writeIn(interets);
		writeIn(lng);
		writeIn(home);
		writeIn(notes);
		writeIn(org);
		writeIn(work);
	}
}

void UserInfoDialog::writeIn(const Category & cat){
	bool catWritten = false;

	std::vector<Info> i = cat.infos;


	std::vector<Info>::iterator dIter(i.begin());
	for (; dIter != i.end(); ++dIter) {
	      wxString value = buddy->getExtendedInfo(dIter->name);
	      if(!value.IsEmpty()){
	    	  if(!catWritten){
	    		  infos->BeginBold();
	    		  infos->WriteText(cat.displayName);
	    		  infos->EndBold();
	    		  infos->Newline();
	    		  catWritten = true;
	    	  }

	    	 infos->WriteText(wxString::Format(dIter->formatString, value.c_str()));

	      }
	}

}

IMPLEMENT_DYNAMIC_CLASS(StdUserInfoDialogModule, iModule);

void StdUserInfoDialogModule::moduleInit(){
	//register Event handler
	Impp* impp = Impp::exemplar();
	impp->Connect(wxID_ANY,EVT_IMPP_COMMAND, ImppCommandEventHandler(StdUserInfoDialogModule::onImppCommand), NULL, this);
	impp->Connect(wxID_ANY,EVT_BUDDY_CHANGED, BuddyChangedEventHandler(StdUserInfoDialogModule::onBuddyChanged), NULL, this);


	//create Chatwindo Button
	iChatWindow* chatWin = impp->getModuleManager()->getChatWindowModule();
	if(chatWin != NULL){
		chatWin->addActionButtion(STD_USERINFO_MODULE_NAME, STD_USERINFO_MODULE_ACTION_SHOW, wxT("show Userinfo"), wxT("show Userinfo"));
	}else{
		wxLogDebug(wxT("UserInfoEvtHandler: No Chatwindow found to add Button!"));
	}

	//create BuddyMenuEntry
	iUserInterface* ui = impp->getModuleManager()->getUserInterfaceModule();
	if(ui != NULL){
		ui->reigsterBuddyMenuEntryAdder(STD_USERINFO_MODULE_NAME, StdUserInfoDialogModule::addBuddyMenuItem);
		ui->getIcons()->registerActionIcon(STD_USERINFO_MODULE_NAME, STD_USERINFO_MODULE_ACTION_SHOW, wxT("icons/chat/userinfo.png"));
	}else{
		wxLogDebug(wxT("UserInfoEvtHandler: No UserInterface found to add Buddy Menu entry!"));
	}

}
void StdUserInfoDialogModule::moduleDestroy(){
	Impp::exemplar()->Disconnect(wxID_ANY,EVT_IMPP_COMMAND, ImppCommandEventHandler(StdUserInfoDialogModule::onImppCommand), NULL, this);
	Impp::exemplar()->Disconnect(wxID_ANY,EVT_BUDDY_CHANGED, BuddyChangedEventHandler(StdUserInfoDialogModule::onBuddyChanged), NULL, this);
}

void StdUserInfoDialogModule::onImppCommand(ImppCommandEvent & event){
	if(event.getModuleName() == STD_USERINFO_MODULE_NAME){
		if(event.getActionName() == STD_USERINFO_MODULE_ACTION_SHOW){
			Buddy* buddy = event.getBuddy();
			wxASSERT(buddy!=NULL);

			iBuddyList* bl = Impp::exemplar()->getModuleManager()->getBuddyListModule();
			if(bl != NULL){
				Impp::exemplar()->requestFullUserInfo(buddy);
				new UserInfoDialog(bl->getWindow(), buddy);
			}else{
				wxLogDebug(wxT("UserInfoEvtHandler: No Buddylist found to show Userinfo!"));
			}

		}
	}else{
		event.Skip();
	}
}

void StdUserInfoDialogModule::onBuddyChanged(BuddyChangedEvent & event){
	if(event.getChangeType() ==  BUDDY_CHANGED_FULL_USERINFO){
		UserInfoDialog::updateOpenWindows(event.getBuddy());
	}
	event.Skip();
}

void wxCALLBACK StdUserInfoDialogModule::addBuddyMenuItem(iMenu* menu, Buddy* buddy){
	menu->appendItem(STD_USERINFO_MODULE_ACTION_SHOW, wxT("show Userinfo"), 20, wxT("show Userinfo"));
}


}
